package com.app.newiptv.helper;


import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.app.newiptv.bean.ChannelType;
import com.app.newiptv.bean.RequestDto;
import com.app.newiptv.bean.VodListBean;
import com.app.newiptv.callback.IRequestCallback;
import com.app.newiptv.callback.IRequestLisenter;
import com.app.newiptv.utils.OkGoUtil;
import com.app.newiptv.utils.Paths;
import com.lzy.okhttputils.callback.StringCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by wendal on 2017/9/15.
 */

public class VodListHelper {
    private IRequestCallback callback;

    public VodListHelper() {
    }

    public void setCallback(IRequestCallback callback) {
        this.callback = callback;
    }

    public void loadData(String url) {
//        OkGoUtil<VodListBean> okGoUtil = new OkGoUtil<VodListBean>(VodListBean.class, OkGoUtil.TYPE_DATA, new IRequestLisenter() {
//            @Override
//            public void onSuccessData(RequestDto dto) {
//                if (dto != null) {
//                    if (callback != null) {
//                        callback.onLoadComplete("", 0, 0, dto.getData());
//                    }
//                }
//            }
//
//            @Override
//            public void onErrer(int code, String errMsg) {
//                if (callback != null) {
//                    callback.onFailed(code + "", errMsg);
//                }
//            }
//        });
//        okGoUtil.requestGet(url);
        // TODO: hailong.qiu
        OkGoUtil<VodListBean> okGoUtil = new OkGoUtil<VodListBean>(null, OkGoUtil.TYPE_DATA, null);
        okGoUtil.requestNewPost(url, new StringCallback() {
            @Override
            public void onSuccess(String result, Call call, Response response) {
                Log.d("tag", "----------" + result);
                if (!TextUtils.isEmpty(result)) {
                    try {
                        ChannelType channelTypeInfo = JSON.parseObject(result, ChannelType.class);
                        List<ChannelType.DataBean> dataBeanList = channelTypeInfo.getData();
                        if (null != callback) {
                            callback.onLoadComplete("", 0, 0, dataBeanList);
                        }
                    } catch (Exception e) {}
                } else {
                    if (null != callback) {
                        callback.onFailed(10001 + "", "result == null");
                    }
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                if (null != callback) {
//                    callback.onFailed(response.code() + "", response.message());
                }
            }
        });
    }
}
